package bs.app.Util;

import java.util.ArrayList;

import android.graphics.Paint;
import android.graphics.Typeface;
import bs.app.android.Font;
import bs.app.android.Graphics;
import bs.app.android.Static;

public class BitMapFont {
	public int m_iFontHeight = 0;
	public int m_iFontColor = 0xFF000000;
	public int m_iBorderColor = 0xFF000000;
	public boolean m_hasBorder = true;

	private int m_iTotalChars = 0;
	private char m_aEnumChars[] = null; // array that stores all chars in game
	private int m_aiFontImageData[] = null; // array that stores data of font's
											// image
	private byte m_bFontWidth[] = null; // array that stores width of all chars
										// in game
	private byte m_abFontPackData[][] = null; // array that stores font data of
												// all chars in game

	private int m_iBitPerPixel = 1;
	private int m_iBitToShift = 0;
	private int m_iBitAND = 0;
	private int m_iBitToCheckBorder = 0;
	private int m_iScale = 0;

	public static Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	
	public Typeface typefont;
	public int iType;
	public static int FONT_MEDIUM = 15;
	public static int FONT_SMALL = 10;
	
	public BitMapFont() {
	}

	public BitMapFont(String strPackageName, int type) {		
		typefont = Typeface.createFromAsset(Static.App.getAssets(),"font/MyriadPro_Regular.otf");
		iType = type;
		// LoadFont(strPackageName);
	}

	public void SetColor(int iNewColor) {
		m_iFontColor = iNewColor | 0xFF000000;

	}

	public void SetBorderColor(int iNewColor) {
		m_hasBorder = true;
		m_iBorderColor = iNewColor | 0xFF000000;
	}

	public void SetBorderHidden() {
		m_hasBorder = false;
		// m_iBorderColor = 0x00FF00FF;
	}

	public int GetColor() {
		return m_iFontColor;
	}

	/*public int DrawChar(Graphics g, char ch, int x, int y) {
		int iCharIndex = MapChar(ch);

		if (iCharIndex == -1) {
			return 0;
		}

		DecodeFont(iCharIndex, m_aiFontImageData);
		int iCharWidth = m_bFontWidth[iCharIndex];
		g.drawRGB(m_aiFontImageData, 0, iCharWidth, x, y, iCharWidth,
				m_iFontHeight, true);

		return iCharWidth;
	}

	public int DrawCharPixel(Graphics g, char ch, int x, int y) {
		int iCharIndex = MapChar(ch);

		if (iCharIndex == -1) {
			return 0;
		}

		int iCharWidth = m_bFontWidth[iCharIndex];
		int iCharSize = m_iFontHeight * iCharWidth;
		int iByteOfPixel = 0;
		int iBitIndex = 0;
		int iDrawingX = x;

		// If font has no border -> need to call setColor() only 1 time.
		g.setColor(m_iFontColor);
		for (int i = 0; i < iCharSize; i++) {
			iByteOfPixel = i >> m_iBitToShift;
			iBitIndex = ((i & m_iBitAND) << m_iScale);

			if (((m_abFontPackData[iCharIndex][iByteOfPixel] >> iBitIndex) & m_iBitToCheckBorder) == 1) {
				g.drawLine(iDrawingX, y, iDrawingX, y); // Draw 1 pixel
			} else if (m_hasBorder
					&& (((m_abFontPackData[iCharIndex][iByteOfPixel] >> iBitIndex) & m_iBitToCheckBorder) == 2)) {
				g.setColor(m_iBorderColor);
				g.drawLine(iDrawingX, y, iDrawingX, y); // Draw 1 pixel
				g.setColor(m_iFontColor);
			}

			iDrawingX++;
			if ((iDrawingX - x) >= iCharWidth) {
				iDrawingX = x;
				y++;
			}
		}

		return iCharWidth;
	}*/

	public int GetFontHeight() {
//		return m_iFontHeight; namnv close
		if(iType == 0)
			return FONT_MEDIUM;
		else 
			return FONT_SMALL;
	}

	/*public int GetCharWidth(char ch) {
		int iCharIndex = MapChar(ch);

		if (iCharIndex == -1) {
			return 0;
		}
		return m_bFontWidth[iCharIndex];
		String s = ch + "";
		Font.paint.setTypeface(typefont);
		Font.paint.setColor(m_iFontColor);
		if(iType == 0)
			Font.paint.setTextSize(FONT_MEDIUM);
		else 
			Font.paint.setTextSize(FONT_SMALL);
		Font.paint.setAntiAlias(true);
		return (int) Font.paint.measureText(s);
	}*/

/*	private int MapChar(char ch) {
		int iLeftIndex = 0;
		int iRightIndex = m_iTotalChars - 1;
		int iMiddleIndex = 0;

		while (iLeftIndex <= iRightIndex) {
			iMiddleIndex = (iLeftIndex + iRightIndex) >> 1;

			if (m_aEnumChars[iMiddleIndex] == ch) {
				return iMiddleIndex;
			} else if (m_aEnumChars[iMiddleIndex] < ch) {
				iLeftIndex = iMiddleIndex + 1;
			} else {
				iRightIndex = iMiddleIndex - 1;
			}
		}
		return -1;
	}*/

	/*private void DecodeFont(int iCharID, int[] aiRGB) {
		int iCharSize = m_iFontHeight * m_bFontWidth[iCharID];
		int iByteOfPixel = 0;
		int iBitIndex = 0;
		// int shift_bit = 0;
		if (aiRGB == null) {
			aiRGB = new int[iCharSize];
		}

		for (int i = 0; i < iCharSize; i++) {
			iByteOfPixel = i >> m_iBitToShift;
			iBitIndex = ((i & m_iBitAND) << m_iScale);

			if (((m_abFontPackData[iCharID][iByteOfPixel] >> iBitIndex) & m_iBitToCheckBorder) == 1) {
				aiRGB[i] = m_iFontColor | 0x0F000000;
			} else if (m_hasBorder
					&& (((m_abFontPackData[iCharID][iByteOfPixel] >> iBitIndex) & m_iBitToCheckBorder) == 2)) {
				aiRGB[i] = m_iBorderColor | 0x0F000000;
			} else {
				aiRGB[i] = 0;
			}
		}
	}*/

	public void drawString(Graphics g, String st, int startX, int startY, int align, int color, int bordercolor, boolean setborder) {
		SetColor(color);
		
		int x = startX;
		int y = startY;
		/*for (int i = 0; i < st.length(); i++) {
			int c = (int) (st.charAt(i));
			char ch = (char) c;
			DrawChar(g, ch, x, y);
			x = x + GetCharWidth(ch);
			if (ch == ' ') {
				x += 3;// space
			}
		}*/
		Font.paint.setTypeface(typefont);
		Font.paint.setColor(m_iFontColor);		
		if(iType == 0)
			Font.paint.setTextSize(FONT_MEDIUM);
		else 
			Font.paint.setTextSize(FONT_SMALL);
		if (setborder == true) {
			SetBorderColor(bordercolor);
			g.drawStringStore(st, x, y, align);
		} else {
			SetBorderHidden();
			g.drawString(st, x, y, align);
		}
		
		
	}
	
	public int stringWidth(String st) {
		int len = 0;
		/*for (int i = 0; i < st.length(); i++) {
			int c = (int) (st.charAt(i));
			char ch = (char) c;
			len += GetCharWidth(ch) + 1;
			if (ch == ' ') {
				len += 2;// space
			}
		}*/
		Font.paint.setTextSize(FONT_MEDIUM);
		Font.paint.setTypeface(typefont);
		Font.paint.setColor(m_iFontColor);
		Font.paint.setAntiAlias(true);
		len =  (int) Font.paint.measureText(st);
		return len;
	}

	public String[] splitStrInLine(String src, int lineWidth) {
		
		if (src == null || src.equals("") || src.length() <= 0)
			return null;
		ArrayList<String> list = new ArrayList<String>();
		int srclen = src.length();
		String tem = "";
		int start = 0, end = 0;
		
		while (true) {
			while (stringWidth(tem) < lineWidth) {
				tem += src.charAt(end);
				if (end == srclen - 1)
					break;
				end++;
			}
			if (end != srclen - 1 && (src.charAt(end + 1) != ' ')) {
				int endAnyway = end;
				while (true) {
					if (src.charAt(end + 1) == ' ' && src.charAt(end) != ' ')
						break;
					if (end == start)
						break;
					end--;
				}
				if (end == start)
					end = endAnyway;
			}
			list.add(src.substring(start, end + 1));
			if (end == srclen - 1)
				break;
			start = end + 1;
			while (start != srclen - 1 && src.charAt(start) == ' ')
				start++;
			if (start == srclen - 1)
				break;
			end = start;
			tem = "";
		}
		String[] strs = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			strs[i] = (String) list.get(i);
		}
		return strs;
		/*Vector list = new Vector();
		int srclen = src.length();
		String tem = "";
		int start = 0, end = 0;
		while (true) {
			while (stringWidth(tem) < lineWidth) {
				if (end == srclen - 1) {
					break;
				}
				tem += src.charAt(end);
				end++;
			}
			if (end != srclen - 1 && (src.charAt(end + 1) != ' ')) {
				int endAnyway = end;
				while (true) {
					if (src.charAt(end + 1) == ' ' && src.charAt(end) != ' ')
						break;
					if (end == start)
						break;
					end--;
				}
				if (end == start)
					end = endAnyway;
			}
			list.addElement(src.substring(start, end + 1));
			if (end == srclen - 1)
				break;
			start = end + 1;
			while (start != srclen - 1 && src.charAt(start) == ' ')
				start++;
			if (start == srclen - 1)
				break;
			end = start;
			tem = "";
		}
		String[] strs = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			strs[i] = (String) list.elementAt(i);
		}
		return strs;*/ //namnv close
		
	}

}
